package com.lv.dhart.ui.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lv.dhart.R;
import com.lv.dhart.beans.JJHoldStockListBeans;
import com.lv.dhart.callback.IJJHoldStockListCallback;
import com.lv.dhart.custom.fragment.baseframework.BaseActivity;
import com.lv.dhart.custom.fragment.baseframework.interfaces.Layout;
import com.lv.dhart.custom.fragment.baseframework.util.JumpParameter;
import com.lv.dhart.holder.activity.jjholdstocklist.JJHoldStockListViewHolderHelper;
import com.lv.dhart.presenter.JJHoldStockListPresenter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.smart.holder.CommonAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Layout(R.layout.activity_jjhold_stock_list)
public class JJHoldStockListActivity extends BaseActivity implements AdapterView.OnItemClickListener, OnRefreshListener, OnLoadMoreListener, IJJHoldStockListCallback {
    protected ListView mListView;
    protected TextView titleHolderNameTv;
    private final List<JJHoldStockListBeans.DataDTO.RowsDTO> mDataBeanList = new ArrayList<>();
    private CommonAdapter<JJHoldStockListBeans.DataDTO.RowsDTO> commonAdapter;
    private RefreshLayout refreshLayout;
    private JJHoldStockListPresenter jjHoldStockListPresenter = new JJHoldStockListPresenter();
    private int offset = 0;
    private int limit = 20;
    HashMap<String, String> params;

    @Override

    public void initViews() {
        mListView = findViewById(R.id.home_common_lv);
        titleHolderNameTv = findViewById(R.id.title_holder_name_tv);
        refreshLayout = (RefreshLayout) findViewById(R.id.common_refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setEnableLoadMore(true);
    }

    @Override
    public void initDatas(JumpParameter parameter) {
        commonAdapter = new CommonAdapter<>(this, mDataBeanList, R.layout.item_activity_hold_stock_list, new JJHoldStockListViewHolderHelper());
        mListView.setAdapter(commonAdapter);
        mListView.setOnItemClickListener(this);
        String jj_name = getIntent().getStringExtra("jj_name");
        titleHolderNameTv.setText(jj_name);
        params = new HashMap<>();
        params.put("jj_name", jj_name);
        jjHoldStockListPresenter.getJJHoldStockList(offset, limit, params, this);
    }

    @Override
    public void setEvents() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        offset = 0;
        jjHoldStockListPresenter.getJJHoldStockList(offset, limit, params, this);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        jjHoldStockListPresenter.getJJHoldStockList(offset, limit, params, this);
    }

    @Override
    public void onIJJHoldStockListSuccess(JJHoldStockListBeans costChangeBeans) {
        if (offset == 0) {
            mDataBeanList.clear();
        }
        offset = costChangeBeans.getOffset() + limit;
        mDataBeanList.addAll(costChangeBeans.getData().getRows());
        commonAdapter.notifyDataSetChanged();
        refreshLayout.finishRefresh(true);
        refreshLayout.finishLoadMore(true);
    }

    @Override
    public void onIJJHoldStockListFailure(Throwable throwable) {
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(true);
    }
}